package com.hua.gulimall.product.controller;

import com.hua.common.utils.PageUtils;
import com.hua.common.utils.R;
import com.hua.gulimall.product.entity.AttrEntity;
import com.hua.gulimall.product.entity.ProductAttrValueEntity;
import com.hua.gulimall.product.service.AttrGroupService;
import com.hua.gulimall.product.service.AttrService;
import com.hua.gulimall.product.service.CategoryService;
import com.hua.gulimall.product.service.ProductAttrValueService;
import com.hua.gulimall.product.vo.AttrRespVo;
import com.hua.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品属性
 *
 * @author ahua
 * @email 123456@163.com
 * @date 2021-11-15 19:20:12
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * /product/attr/base/listforspu/{spuId}
     */
    @GetMapping("/base/listforspu/list/{spuId}")
    public R listForSpu(@PathVariable("spuId") Long spuId) {
       List<ProductAttrValueEntity> baseProductAttr = productAttrValueService.baseListForSpu(spuId);

        return R.ok().put("data", baseProductAttr);
    }
    /**
     * /product/attr/update/{spuId}
     * 修改规格参数
     */
    @PostMapping("/update/{spuId}")
    public R updateProductAttr(@PathVariable("spuId") Long spuId,
                               @RequestBody List<ProductAttrValueEntity> attrValueEntitys){
        productAttrValueService.updateProductAttr(spuId,attrValueEntitys);

        return R.ok();
    }

    /**
     * 规格参数列表
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R getBaseAttrList(@RequestParam Map<String, Object> params,
                             @PathVariable("catelogId") Long catelogId,
                             @PathVariable("attrType") String type) {
        //PageUtils page = attrService.queryPage(params);
        PageUtils page;
        if ("base".equals(type)){
            page = attrService.queryBaseAttrPage(params, catelogId);
        }else{
            page = attrService.querySaleAttrPage(params, catelogId);
        }
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
        AttrRespVo attr = attrService.getAttrInfo(attrId);

        Long[] path = categoryService.findCateLogPath(attr.getCatelogId());
        attr.setCatelogPath(path);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attrVo) {
        //attrService.save(attr);
        attrService.saveAttr(attrVo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr) {
        attrService.updateBaseAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
